package com.yunxitest.yunxitest.item.sim

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import com.yunxitest.yunxitest.Interface.IDetectItem
import com.yunxitest.yunxitest.Interface.IResultCallback
import com.yunxitest.yunxitest.utils.L

class SimDetectManager(val context: Context, val callback: IResultCallback) : IDetectItem {

    fun isMobileConnected(context: Context): Boolean {
        val mConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isAvailable();
        }
        return false
    }

    /**
     * 判断移动网络是否连接
     * @param context
     * @return
     */
    fun isMobileConnected():Boolean{
        var connectivityManager:ConnectivityManager= context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        var networkInfo:NetworkInfo = connectivityManager .getNetworkInfo (ConnectivityManager.TYPE_MOBILE);

        return networkInfo.isConnected
    }


    override fun startDetect() {
        val isConncect = isMobileConnected()
        if (isConncect) {
            callback.success()
            L.d("testItem", " test sim successful .")
        } else {
            callback.fail("no mobile network")
            L.d("testItem", " test sim fail : no mobile network")
        }
    }

    override fun finish() {
        // no thing
    }
}